#coding=utf-8
import codecs # To support chinese
import json
import re
import requests
class  xinlang():
    coo ='UOR=www.baidu.com,finance.sina.com.cn,; SINAGLOBAL=101.71.39.183_1665823636.296906; Apache=101.71.39.183_1665823636.296908; ULV=1665823637876:1:1:1:101.71.39.183_1665823636.296908:; hqEtagMode=1; SUB=_2A25OTgPpDeRhGeNI7FsX9SnPyDmIHXVtOnIhrDV_PUNbm9ANLXTAkW9NSAygB5l_voVOnfVtW2ualOk4Zo97ornW; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WFhY9Cvr3.zAyCLjJBrb-1G5JpX5KzhUgL.Fo-cS0.cSKM0e0-2dJLoI0qLxK.L1KBL12zLxKnL1h5L1h2LxK-LBKBLBoBLxKnLBKML1hzLxKBLBonL1KqLxKML1K-L12et; ALF=1697359673; rotatecount=3; WATCHLIST-FINANCE-SINA-COM-CN=; U_TRS1=0000005b.3a944b3a.634a73bc.7a50def3; U_TRS2=0000005b.3a9f4b3a.634a73bc.ea8bb59e'

    def group_create (self,groupName) :
        pid = None
        #coo ='UOR=www.baidu.com,finance.sina.com.cn,; SINAGLOBAL=125.118.2.55_1665312441.264561; SR_SEL=1_511; FIN_ALL_VISITED=sz000948; FINA_V_S_2=sz000948; U_TRS1=00000037.99b7d417.6342a6dc.cafa4849; SFA_version=2021-08-02%2009%3A00; Apache=125.119.220.219_1665642683.655698; SCF=Akx0EAaoV4ZsnXs9Jxsmo7-gGZgrqul-lfcAdDcc-P9M9mlaifMW8kUmm5ytwoTLO66QNSMnR9ubZn0VMpvd548.; SUB=_2A25OQ8DuDeRhGeNI7FsX9SnPyDmIHXVtOLUmrDV_PUNbm9AKLXnwkW9NSAygB1-e9VHYX2dLvIl-SmQoRBhVI63S; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WFhY9Cvr3.zAyCLjJBrb-1G5NHD95QfSoM4So-Ne0efWs4Dqcjci--4iK.XiKyFi--RiKn7iKnpi--fi-2Xi-zXi--Ri-2NiKnEi--Xi-zRiK.ci--NiK.fiKyh; ALF=1697178686; WATCHLIST-FINANCE-SINA-COM-CN=; U_TRS2=000000db.6a9317d9.6347b0bf.e8712d6d; ULV=1665642697985:5:5:5:125.119.220.219_1665642683.655698:1665642683346; hqEtagMode=1'
        try:
            headers = {
                'Accept':'application/json, text/javascript, */*; q=0.01',
                'Accept-Encoding':'gzip, deflate',
                'Accept-Language':'zh-CN,zh;q=0.9',
                 #'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
                 #'Host': 'i.money.163.com',
                 #'Origin': 'http://i.money.163.com',
                'Referer': 'https://i.finance.sina.com.cn/',
                'Cookie': xinlang.coo,
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36'}
            url = 'https://watchlist.finance.sina.com.cn/portfolio/api/openapi.php/PortfolioService.createPortfolio?callback=jQuery111105592809959212857_1665642707546&name={a}'
            url = url.format(a=groupName)
            data = requests.get ( url, headers=headers )
            str=data.text
            a = re.findall('\\((.*?)\\)', str)
            if len(a)>0:
               json_object = json.loads(a[0])
               pid = json_object['result']['data']['pid']
               return pid
        except Exception as err:
            return None

    def add (self,name,pid) :
        #coo ='UOR=www.baidu.com,finance.sina.com.cn,; SINAGLOBAL=125.118.2.55_1665312441.264561; SR_SEL=1_511; FIN_ALL_VISITED=sz000948; FINA_V_S_2=sz000948; U_TRS1=00000037.99b7d417.6342a6dc.cafa4849; SFA_version=2021-08-02%2009%3A00; Apache=125.119.220.219_1665642683.655698; SCF=Akx0EAaoV4ZsnXs9Jxsmo7-gGZgrqul-lfcAdDcc-P9M9mlaifMW8kUmm5ytwoTLO66QNSMnR9ubZn0VMpvd548.; SUB=_2A25OQ8DuDeRhGeNI7FsX9SnPyDmIHXVtOLUmrDV_PUNbm9AKLXnwkW9NSAygB1-e9VHYX2dLvIl-SmQoRBhVI63S; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WFhY9Cvr3.zAyCLjJBrb-1G5NHD95QfSoM4So-Ne0efWs4Dqcjci--4iK.XiKyFi--RiKn7iKnpi--fi-2Xi-zXi--Ri-2NiKnEi--Xi-zRiK.ci--NiK.fiKyh; ALF=1697178686; WATCHLIST-FINANCE-SINA-COM-CN=; U_TRS2=000000db.6a9317d9.6347b0bf.e8712d6d; ULV=1665642697985:5:5:5:125.119.220.219_1665642683.655698:1665642683346; hqEtagMode=1'
        try:
            headers = {
                'Accept':'application/json, text/javascript, */*; q=0.01',
                'Accept-Encoding':'gzip, deflate',
                'Accept-Language':'zh-CN,zh;q=0.9',
                 #'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
                 #'Host': 'i.money.163.com',
                 #'Origin': 'http://i.money.163.com',
                'Referer': 'https://i.finance.sina.com.cn/',
                'Cookie':  xinlang.coo,
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36'}
            url = 'https://watchlist.finance.sina.com.cn/portfolio/api/openapi.php/HoldV2Service.appendSymbol?callback=jQuery1111009953807724314467_1665652149194&scode={name}%40cn&source=pc_mzx&pid={pid}'
            url = url.format(name=name,pid=pid)
            data = requests.get ( url, headers=headers )
            print (data.text)
        except Exception as err:
            print ( "Finished with error: ", err )
            return None

    def delgp (self,pid) :
        #coo ='UOR=www.baidu.com,finance.sina.com.cn,; SINAGLOBAL=125.118.2.55_1665312441.264561; SR_SEL=1_511; FIN_ALL_VISITED=sz000948; FINA_V_S_2=sz000948; U_TRS1=00000037.99b7d417.6342a6dc.cafa4849; SFA_version=2021-08-02%2009%3A00; Apache=125.119.220.219_1665642683.655698; SCF=Akx0EAaoV4ZsnXs9Jxsmo7-gGZgrqul-lfcAdDcc-P9M9mlaifMW8kUmm5ytwoTLO66QNSMnR9ubZn0VMpvd548.; SUB=_2A25OQ8DuDeRhGeNI7FsX9SnPyDmIHXVtOLUmrDV_PUNbm9AKLXnwkW9NSAygB1-e9VHYX2dLvIl-SmQoRBhVI63S; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WFhY9Cvr3.zAyCLjJBrb-1G5NHD95QfSoM4So-Ne0efWs4Dqcjci--4iK.XiKyFi--RiKn7iKnpi--fi-2Xi-zXi--Ri-2NiKnEi--Xi-zRiK.ci--NiK.fiKyh; ALF=1697178686; WATCHLIST-FINANCE-SINA-COM-CN=; U_TRS2=000000db.6a9317d9.6347b0bf.e8712d6d; ULV=1665642697985:5:5:5:125.119.220.219_1665642683.655698:1665642683346; hqEtagMode=1'
        try:
            #body= { 'groupId':'7432927','code':'0600000' }
            headers = {
                'Accept':'application/json, text/javascript, */*; q=0.01',
                'Accept-Encoding':'gzip, deflate',
                'Accept-Language':'zh-CN,zh;q=0.9',
                 #'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
                 #'Host': 'i.money.163.com',
                 #'Origin': 'http://i.money.163.com',
                'Referer': 'https://i.finance.sina.com.cn/',
                'Cookie':  xinlang.coo,
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36'}
            url = 'https://watchlist.finance.sina.com.cn/portfolio/api/openapi.php/PortfolioService.delPortfolio?callback=jQuery111103081521536564915_1665652323371&pid=%s' % pid
            data = requests.get ( url, headers=headers )
            print (data.text)
        except Exception as err:
            print ( "Finished with error: ", err )
            return None


if __name__ == "__main__":
    #coo = 'UOR=www.baidu.com,finance.sina.com.cn,; SINAGLOBAL=125.118.2.55_1665312441.264561; SR_SEL=1_511; FIN_ALL_VISITED=sz000948; FINA_V_S_2=sz000948; U_TRS1=00000037.99b7d417.6342a6dc.cafa4849; SFA_version=2021-08-02%2009%3A00; Apache=125.119.220.219_1665642683.655698; SCF=Akx0EAaoV4ZsnXs9Jxsmo7-gGZgrqul-lfcAdDcc-P9M9mlaifMW8kUmm5ytwoTLO66QNSMnR9ubZn0VMpvd548.; SUB=_2A25OQ8DuDeRhGeNI7FsX9SnPyDmIHXVtOLUmrDV_PUNbm9AKLXnwkW9NSAygB1-e9VHYX2dLvIl-SmQoRBhVI63S; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WFhY9Cvr3.zAyCLjJBrb-1G5NHD95QfSoM4So-Ne0efWs4Dqcjci--4iK.XiKyFi--RiKn7iKnpi--fi-2Xi-zXi--Ri-2NiKnEi--Xi-zRiK.ci--NiK.fiKyh; ALF=1697178686; WATCHLIST-FINANCE-SINA-COM-CN=; U_TRS2=000000db.6a9317d9.6347b0bf.e8712d6d; ULV=1665642697985:5:5:5:125.119.220.219_1665642683.655698:1665642683346; hqEtagMode=1'
    xinlang = xinlang()
    pid = xinlang.group_create('3999')



    xinlang.add('sh600519', pid)
    print(pid)



    #money163 =money163()
    #money163.xq('601899')
